home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / me_cd22.zip / MUTT2.ZIP / DIR.MUT < prev    next >
Text File  |  1992-04-27  |  4KB  |  142 lines

  1.   ;; dir.mut : directory stack stuff for UNIX versions of ME
  2.   ;; pgms defined:
  3.   ;; - cd   :  Change the current directory.
  4.   ;; - dirs :  Show the contents of the directory stack.  The top (or left
  5.   ;;     most) entry is the current directory, next is the directory you
  6.   ;;     left to get to the current directory.
  7.   ;; - pwd  :  Show the current directory.
  8.   ;; - pu   :  Change the current directory, using the stack to save the
  9.   ;;     changes.
  10.   ;;     (pu directory) pushes the current directory on the stack and sets
  11.   ;;       the current directory to directory.
  12.   ;;     (pu "") swaps the top 2 entries on the stack.
  13.   ;;     (pu +n) swaps the top of the stack with the nth stack item.  n
  14.   ;;       starts at zero.
  15.   ;; - po discards the top entry on the stack and sets the current
  16.   ;;     directory to the new top of stack.
  17.   ;; C Durland 10/87, 5/88    Public Domain
  18.  
  19. (include me2.h)
  20.  
  21. (const stack-size 10)    ; the max number of entries on the directory stack
  22.  
  23. (small-int num-stack-entries)
  24.  
  25. (defun
  26.   init-dirstack MAIN { (clear-stack) (push (sub~ (current-directory))) }
  27.   cd
  28.   {
  29.     (if (current-directory (complete CC_FNAME "change directory to: "))
  30.       {
  31.     (pop)    ;; nuke the first entry to make room for this one
  32.     (msg "moved to " (push (sub~ (current-directory))))
  33.     TRUE
  34.       }
  35.       { (msg "Not a valid directory") FALSE })
  36.   }
  37.   pwd { (msg "current directory: " (sub~ (current-directory))) }
  38.   pu        ;; push directory
  39.   {
  40.     (string name name2)
  41.     (int n)
  42.  
  43.     (name (complete CC_FNAME "pushd to: "))
  44.     (if (== name "")    ; swap top 2 items on stack
  45.     {
  46.       (if (< num-stack-entries 2)
  47.     { (msg "directory stack too small to swap") FALSE (done) })
  48.       (name (pop)) (name2 (pop))
  49.       (push name)  (push name2)
  50.  
  51.       (cd name2)
  52.       (done)
  53.     })
  54.  
  55.     (if (== (extract-element name 0) "+")   ; +n => move (n+1)th entry to top
  56.     {
  57.       (n (convert-to NUMBER (extract-elements name 1 10000)))
  58.       (if (>= n num-stack-entries)
  59.     { (msg "Only got " num-stack-entries " entries")(done) })
  60.       (name (popn n))
  61.       (push name)    ;; push a dummy that is nuked my (cd)
  62.  
  63.       (cd name)
  64.       (done)
  65.     })
  66.     (if (current-directory name)    ;; directory exists
  67.       {
  68.     (if (>= num-stack-entries stack-size)    ;; shrink stack
  69.         (popn (- num-stack-entries 1)))
  70.         (msg "moved to " (push (sub~ (current-directory))))
  71.     TRUE
  72.       }
  73.       { (msg "Not a valid directory") FALSE })
  74.   }
  75.   po        ;; pop directory
  76.   {
  77.     (if (< num-stack-entries 2) { (msg "directory stack empty") FALSE (done) })
  78.     (pop)
  79.     (cd (peek 0))
  80.   }
  81.   sub~ (string dir-name) HIDDEN    ; convert /users/craig/... to ~/...
  82.   {
  83.     (int n)
  84.  
  85.     (n (length-of (getenv "HOME")))
  86.     (if (and (!= n 0) (== (getenv "HOME") (extract-elements dir-name 0 n)))
  87.     (concat "~" (extract-elements dir-name n 1000))
  88.     dir-name
  89.     )
  90.   }
  91. )
  92.  
  93.  
  94. (defun
  95.   MAIN
  96.   {
  97.     (require "popup")        ;; for (dirs)
  98.   }
  99.   dirs
  100.   {
  101.     (int j)
  102.  
  103.     (menu-box
  104.       ">Directory Stack"
  105.       "-"
  106.       (for (j 0) (< j num-stack-entries)(+= j 1) (push-arg (peek j)))
  107.     )
  108.   }
  109. )
  110.  
  111.  
  112.  
  113. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  114. ;;;;;;;;;;;;; Stack Details ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  115. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  116.  
  117. (list dir-stack)
  118.  
  119. (defun
  120.   clear-stack HIDDEN
  121.   {
  122.     (while (!= 0 num-stack-entries) (pop))
  123.   }
  124.   push (string dir) HIDDEN        ;; returns name
  125.   {
  126.     (insert-object dir-stack -1 dir)
  127.     (num-stack-entries (length-of dir-stack))
  128.     dir
  129.   }
  130.   peek (int n) HIDDEN { (extract-element dir-stack n) }
  131.   popn (int n) HIDDEN
  132.   {
  133.     (string dir)
  134.  
  135.     (dir (extract-element dir-stack n))
  136.     (remove-elements dir-stack n 1)
  137.     (num-stack-entries (length-of dir-stack))
  138.     dir
  139.   }
  140.   pop HIDDEN { (popn 0) }
  141. )
  142.